package com.java.yz.day15;

import java.util.Timer;
import java.util.TimerTask;

/*
    定时器: Timer线程调度任务以供将来在后台线程中执行的功能。任务可以安排一次执行，或定期重复执行。
    构造方法:
    public Timer()

    public void schedule(TimerTask task, long delay)
    public void schedule(TimerTask task,long delay,long period)

 */
public class TimeThreadDemo1 {
    public static void main(String[] args) {
//        Timer t1 = new Timer();
//        t1.schedule(new MyTimeTask(t1),5000);

        //timer.cancel();//取消定时器

        //public void schedule(TimerTask task, long delay) 延迟delay毫秒后，做某一件事请task

        //public void schedule(TimerTask task,long delay,long period)
        //延迟delay毫秒后，做某一件事情，然后每间隔period毫秒重复执行
        Timer t2 = new Timer();
        t2.schedule(new MyTimeTask(t2),5000L,1000);


    }
}

//class MyTimeTask extends TimerTask{
//    private Timer timer;
//
//    public MyTimeTask(Timer timer) {
//        this.timer = timer;
//    }
//
//    @Override
//    public void run() {
//        System.out.println("我醒了");
//        timer.cancel();
//        System.out.println("定时器结束");
//    }
//}


class MyTimeTask extends TimerTask{
    private Timer timer;

    public MyTimeTask(Timer timer) {
        this.timer = timer;
    }

    @Override
    public void run() {
        System.out.println("砰！！！！");
        //timer.cancel();
        //System.out.println("定时器结束");
    }
}